"""
Case Type   : DATAVEC模块
Case Name   : PQ插件前置设置
Create At   : 2025/4/10
Owner       : @peilinqian
Description :
    1. 设置pq环境变量;
    2. 修改参数enable_pq为true;
    3. 重启数据库，使参数生效;
Expect      :
    1. 设置pq环境变量; 成功
    2. 修改参数enable_pq为true; 成功
    3. 重启数据库，使参数生效; 重启成功，参数生效;
History     :
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

pri_root = Node(node='PrimaryRoot')
pri_dbuser = Node(node='PrimaryDbUser')
pq_lib_path = '/usr/local/sra_recall/lib/'
log = Logger()
log.info("----判断本地环境是否存在pq插件----")
check_cmd = f'if [ -d {pq_lib_path} ]; ' \
            f'then echo "True"; else echo "False"; fi'
log.info(check_cmd)
check_result = pri_root.sh(check_cmd).result()
log.info(check_result)
check_kuangjia_cmd = f"uname -a"
log.info(check_kuangjia_cmd)
check_kuangjia_result = pri_root.sh(check_kuangjia_cmd).result()
log.info(check_kuangjia_result)


@unittest.skipIf(f"aarch64" not in check_kuangjia_result,
                 '----环境非ARM架构，跳过执行用例----')
class DataVecTestCase(unittest.TestCase):
    def setUp(self):
        log.info(f'----{os.path.basename(__file__)}:start----')
        self.sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.sta2_node = Node(node='Standby2Root')
        self.constant = Constant()
        self.com = Common()
        self.pq_env_name = 'DATAVEC_PQ_LIB_PATH'
        self.file_name = r'BoostKit-SRA_Recall-1.1.0.zip'
        self.cur_path = os.path.join(macro.SCRIPTS_PATH, 'PQ_BoostKit_SRA_Recall')
        self.target_path = r'/usr/local/sra_recall'
        self.target_rpm_path = os.path.join(self.target_path, 
                                    'boostkit-sra_recall*aarch64.rpm')

    def test_main(self):
        step_txt = '----step1:安装加速包; expect:成功----'
        log.info(step_txt)
        if check_result == 'False':
            log.info("----环境不具备PQ插件，安装加速包----")
            install_cmd = f"rm -rf {self.target_path}; mkdir {self.target_path};"
            log.info(install_cmd)
            install_result = pri_root.sh(install_cmd).result()
            log.info(install_result)
            self.com.scp_file(node=pri_dbuser, 
                file_name=self.file_name,
                target_path=self.target_path,
                source_dir=self.cur_path)
            install_cmd = f"cd {self.target_path} && unzip {self.file_name};"
            log.info(install_cmd)
            install_result = pri_root.sh(install_cmd).result()
            log.info(install_result)
            install_cmd = f"rpm -ivf {self.target_rpm_path};"
            log.info(install_cmd)
            install_result = pri_root.sh(install_cmd).result()
            log.info(install_result)
            time.sleep(2)

        step_txt = '----step2:设置pq环境变量; expect:成功----'
        log.info(step_txt)
        check_env_cmd = f'if grep -q "{self.pq_env_name}" ' \
                        f'"{macro.DB_ENV_PATH}"; ' \
                        f'then echo "True" ; else echo "False" ;fi'
        set_env_cmd = f"echo 'export {self.pq_env_name}={pq_lib_path}' " \
                      f">> {macro.DB_ENV_PATH}"
        for node in [self.sh.node, self.sta_1_sh.node, self.sta_2_sh.node]:
            log.info(check_env_cmd)
            check_env_result = node.sh(check_env_cmd).result()
            log.info(check_env_result)
            if check_env_result == 'False':
                log.info('--设置pq环境变量--')
                log.info(set_env_cmd)
                try:
                    result = node.sh(set_env_cmd).result()
                    log.info(f"{node.name} 设置结果: {result}")
                except Exception as e:
                    log.error(f"{node.name} 设置异常: {str(e)}")
            check_env_result = node.sh(check_env_cmd).result()
            log.info(f"{node.name} 环境变量检查结果: {check_env_result}")
            self.assertEqual(check_env_result, 'True', '执行失败:' + step_txt)

        step_txt = '----step3:重启数据库，使参数生效; expect:重启成功，参数生效;----'
        log.info(step_txt)
        restart_result = self.sh.restart_db_cluster()
        self.assertTrue(restart_result)
        log.info('----查询参数----')

    def tearDown(self):
        step_txt = '----this is teardown----'
        log.info(step_txt)
        log.info(f'----{os.path.basename(__file__)}:end----')
